package com.im.controller;

import com.im.service.dto.FuncMenu;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

import com.im.service.dto.Orgarization;
import com.im.service.orgarization.dao.IOrgarizationQueryDao;
import com.im.service.orgarization.smo.IOrgarizationQuerySmo;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.im.service.dto.Staff;
import com.im.service.user.smo.IUserOperSMO;
import com.im.service.user.smo.IUserQuerySMO;
import com.im.util.JsonUtil;
import com.im.util.ReturnsUtil;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

@RestController
@EnableAutoConfiguration
@RequestMapping("/userService")
public class UserService {
	
	private static final Logger logger = LogManager.getLogger(UserService.class); 
	
	@Autowired
	private Environment env;//执行环境
	@Autowired
	private IUserQuerySMO userQuerySMO;
	@Autowired
	private IUserOperSMO userOperSMO;
	@Autowired
	private IOrgarizationQuerySmo orgarizationQuerySmo;
	
	@RequestMapping("/test")
	@ResponseBody
	public String test(@RequestBody String jsonString) {
    logger.debug(jsonString);
    Staff staff = null;
    try {
      //Map<String,Object> param = new HashMap<>();
      //param.put("staffId", "346620c5-e3a6-4c83-9774-a8c953a71bd4");
      Map<String, Object> param = JsonUtil.getMap(jsonString);

      staff = null;//userQuerySMO.queryStaffBaseInfo(param);

      staff.setStaffId("346620c5-e3a6-4c83-9774-a8c953a71bd4");
      staff.setStaffPwd("123");
      userOperSMO.updateStaff(staff);

    } catch (Exception e) {
      e.printStackTrace();
    }

    return ReturnsUtil.returnSuccess(staff).toString();
  }

	/**
	 * 查询员工的基本信息、角色信息、菜单信息、权限信息等
	 * @param request
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryStaffBaseInfo")
	@ResponseBody
	public String queryStaffBaseInfo(HttpServletRequest request,@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			if(!param.containsKey("staffId")){
				//获取员工
				Object staffObj = request.getSession().getAttribute("loginedStaff");
				if(staffObj == null){
					return  ReturnsUtil.returnFailure("未找到员工登录信息！！请重新登录！！");
				}
				param.put("staffId",((Staff)staffObj).getStaffId());
			}

			return ReturnsUtil.returnSuccess(userQuerySMO.queryStaffBaseInfo(param)).toString();
		} catch (Exception e) {
			logger.error("员工基本信息查询异常，{}",e);
			return ReturnsUtil.returnException("员工基本信息查询异常，{}",e).toString();
		}
		
	}

	/**
	 * 查询员工列表信息
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryStaffsByCond")
	@ResponseBody
	public String queryStaffsByCond(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryStaffsByCond(param)).toString();
		} catch (Exception e) {
			logger.error("员工列表信息查询异常，{}",e);
			return ReturnsUtil.returnException("员工列表信息查询异常，{}",e).toString();
		}

	}

	/**
	 * 保存或更新员工信息
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/saveOrUpdateStaff")
	@ResponseBody
	public String saveOrUpdateStaff(@RequestBody JSONObject jsonString) {
		try {
			return ReturnsUtil.returnSuccess(userOperSMO.saveOrUpdateStaff(jsonString)).toString();
		} catch (Exception e) {
			logger.error("员工信息保存或更新查询异常，{}",e);
			return ReturnsUtil.returnException("员工信息保存或更新查询异常，{}",e).toString();
		}
	}
	/**
	 * 查询所有的角色列表配置数据
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryConfigRolesByCond")
	@ResponseBody
	public String queryConfigRolesByCond(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryConfigRolesByCond(param)).toString();
		} catch (Exception e) {
			logger.error("角色列表查询异常，{}",e);
			return ReturnsUtil.returnException("角色列表查询异常，{}",e).toString();
		}

	}
	/**
	 * 查询员工数据维度类型配置数据
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryConfigStaffLimitType")
	@ResponseBody
	public String queryConfigStaffLimitType(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryConfigStaffLimitType(param)).toString();
		} catch (Exception e) {
			logger.error("角色列表查询异常，{}",e);
			return ReturnsUtil.returnException("角色列表查询异常，{}",e).toString();
		}

	}

	/**
	 * 保存或更新员工数据维度信息
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/saveOrUpdateStaffLimit")
	@ResponseBody
	public String saveOrUpdateStaffLimit(@RequestBody JSONObject jsonString) {
		try {
			return ReturnsUtil.returnSuccess(userOperSMO.saveOrUpdateStaffLimit(jsonString)).toString();
		} catch (Exception e) {
			logger.error("员工信息保存或更新查询异常，{}",e);
			return ReturnsUtil.returnException("员工信息保存或更新查询异常，{}",e).toString();
		}
	}
	/**
	 * 查询配置数据维度下的实例数据
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryLimitInstByCond")
	@ResponseBody
	public String queryLimitInstByCond(HttpServletRequest request,@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			//获取员工
			Object staffObj = request.getSession().getAttribute("loginedStaff");
			if(staffObj == null){
				return  ReturnsUtil.returnFailure("未找到员工登录信息！！请重新登录！！");
			}
			param.put("loginedStaffId",((Staff)staffObj).getStaffId());
			return (JSONObject.fromObject(userQuerySMO.queryLimitInstByConn(param))).toString();
		} catch (Exception e) {
			logger.error("角色列表查询异常，{}",e);
			return ReturnsUtil.returnException("角色列表查询异常，{}",e).toString();
		}

	}
	/**
	 * 查询组织
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryOrgarizationsByCond")
	@ResponseBody
	public String queryOrgarizationsByCond(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryOrgarizationsByCond(param)).toString();
		} catch (Exception e) {
			logger.error("组织列表查询异常，{}",e);
			return ReturnsUtil.returnException("组织列表查询异常，{}",e).toString();
		}

	}
	/**
	 * 查询组织-fenye
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryOrgarizationsByCondPage")
	@ResponseBody
	public String queryOrgarizationsByCondPage(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryOrgarizationsByCondPage(param)).toString();
		} catch (Exception e) {
			logger.error("组织列表查询异常，{}",e);
			return ReturnsUtil.returnException("组织列表查询异常，{}",e).toString();
		}

	}
	/**
	 * 保存或更新组织信息
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/saveOrUpdateOrg")
	@ResponseBody
	public String saveOrUpdateOrg(@RequestBody JSONObject jsonString) {
		try {
			return ReturnsUtil.returnSuccess(userOperSMO.saveOrUpdateOrg(jsonString)).toString();
		} catch (Exception e) {
			logger.error("单位信息保存或更新查询异常，{}",e);
			return ReturnsUtil.returnException("单位信息保存或更新查询异常，{}",e).toString();
		}
	}

	/**
	 * 保存或更新角色信息
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/saveOrUpdateRole")
	@ResponseBody
	public String saveOrUpdateRole(@RequestBody JSONObject jsonString) {
		try {
			return ReturnsUtil.returnSuccess(userOperSMO.saveOrUpdateRole(jsonString)).toString();
		} catch (Exception e) {
			logger.error("角色信息保存或更新查询异常，{}",e);
			return ReturnsUtil.returnException("角色信息保存或更新查询异常，{}",e).toString();
		}
	}
	/**
	 * 查询菜单
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryFuncMenusByCond")
	@ResponseBody
	public String queryFuncMenusByCond(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			return ReturnsUtil.returnSuccess(userQuerySMO.queryFuncMenusByCond(param)).toString();
		} catch (Exception e) {
			logger.error("菜单查询异常，{}",e);
			return ReturnsUtil.returnException("菜单查询异常，{}",e).toString();
		}

	}
	/**
	 * 修改或重置密码
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/updateStaffPwd")
	@ResponseBody
	public String updateStaffPwd(HttpServletRequest request,@RequestBody JSONObject jsonString) {
		try {
			//获取员工
			Object staffObj = request.getSession().getAttribute("loginedStaff");
			if(staffObj == null){
				return  ReturnsUtil.returnFailure("未找到员工登录信息！！请重新登录！！");
			}
			return JSONObject.fromObject(userOperSMO.updateStaffPwd((Staff)staffObj,jsonString)).toString();
		} catch (Exception e) {
			logger.error("密码操作失败，{}",e);
			return ReturnsUtil.returnException("密码操作失败，{}",e).toString();
		}
	}

	/**
	 * 入参是:limitTypeId,staffId,rangeValue(可空），chkType(1:查列表，2：查真假）
	 */
	@RequestMapping("/queryStaffRange")
	@ResponseBody
	public String queryStaffRange(@RequestBody JSONObject jsonString) {
		try {
			Map<String,Object> result = userQuerySMO.queryStaffRange(jsonString.toString());
			return ReturnsUtil.returnSuccess(JSONObject.fromObject(result)).toString();
		} catch (Exception e) {
			logger.error("取权限信息异常，{}",e);
			return ReturnsUtil.returnException("取权限信息异常，{}",e).toString();
		}
	}


	/**
	 * 查询员工某个权限维度只
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryStaffLimitInstByCond")
	@ResponseBody
	public String queryStaffLimitInstByCond(HttpServletRequest request,@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			//获取员工
			Object staffObj = request.getSession().getAttribute("loginedStaff");
			if(staffObj == null){
				return  ReturnsUtil.returnFailure("未找到员工登录信息！！请重新登录！！");
			}
			param.put("staffId",((Staff)staffObj).getStaffId());
			Map<String,Object> result = userQuerySMO.queryStaffRange(param.toString());
			return ReturnsUtil.returnSuccess(JSONObject.fromObject(result)).toString();
		} catch (Exception e) {
			logger.error("员工权限维度值异常，{}",e);
			return ReturnsUtil.returnException("员工权限维度值异常，{}",e).toString();
		}

	}

	/**
	 * 查询员工某个权限维度只
	 * @param jsonString
	 * @return
	 */
	@RequestMapping("/queryOrgarizationDetail")
	@ResponseBody
	public String queryOrgarizationDetail(@RequestBody String jsonString) {
		try {
			Map<String, Object> param = JsonUtil.getMap(jsonString);
			Orgarization orgarization = new Orgarization();
			if(param.containsKey("orgId") && !"".equals(param.get("orgId").toString())){
				orgarization.setOrgId(param.get("orgId").toString());
			}
			if(param.containsKey("orgName") && !"".equals(param.get("orgName").toString())){
				orgarization.setOrgName(param.get("orgName").toString());

			}
			orgarization = orgarizationQuerySmo.queryOrgarizationDetail(orgarization);
			if(orgarization == null){
				return ReturnsUtil.returnSuccess("{}").toString();
			}else {
				return ReturnsUtil.returnSuccess(orgarization).toString();
			}
		} catch (Exception e) {
			logger.error("员工权限维度值异常，{}",e);
			return ReturnsUtil.returnException("员工权限维度值异常，{}",e).toString();
		}

	}
}